package com.example.javademo.sys.controller;

import com.example.javademo.sys.entity.PageDTO;
import com.example.javademo.sys.entity.UserQuery;
import com.example.javademo.sys.entity.userInfo;
import com.example.javademo.sys.expection.BizException;
import com.example.javademo.sys.mapper.userMapper;
import com.example.javademo.sys.result.ResponseResult;
import com.example.javademo.sys.result.ResultCode;
import com.example.javademo.sys.service.IuserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


//http://localhost:8081/api/swagger-ui/index.html
//接收表单数据
//https://blog.csdn.net/minihuabei/article/details/134514424
@Slf4j
@RestController
//@Api("login")
@RequestMapping("/user")
public class userController {
    @Autowired
    private userMapper userMapper;

    @Autowired
    private IuserService userService;
    @PostMapping("/loginBody")
    @ResponseResult
    public Map<String, Object> login(@RequestBody userInfo user) {
//        System.out.println("用户名：" + user.getUserName ()+ ", 密码: " + user.getPassWord());
//        return "用户名：" + user.getUserName () + ", 密码: " + user.getPassWord();
//        HashMap<String, Object> data = new HashMap<>();
//        data.put("info", "成功123");
//        return data;
        userInfo userInfo =userService.login(user);
        if(userInfo==null){
            throw new BizException(ResultCode.USER_LOGIN_ERROR);
        }else {
            HashMap<String, Object> data = new HashMap<>();
            data.put("info", userInfo);
            return data;
        }
    }
    //获取用户列表数据 分页
    @GetMapping("/getUserList")
    @ResponseResult
    public Map<String, Object> getUserList(UserQuery query) {
        PageDTO<userInfo> info=userService.queryUsersPage(query);
        HashMap<String, Object> data = new HashMap<>();
        data.put("info", info);
        return data;
    }

    //用户注册
    @PostMapping("/register")
    @ResponseResult
    public Map<String, Object> register(@RequestBody userInfo user) {
        userInfo userInfo=userService.register(user);
        HashMap<String, Object> data = new HashMap<>();
        data.put("info", userInfo);
        return data;
    }



    @PostMapping("/queryLogin")
    @ResponseResult
    public Map<String, Object> queryLogin(@RequestBody userInfo user) {
        System.out.println("queryLogin");
//        System.out.println("用户名：" + user.getUserName ()+ ", 密码: " + user.getPassWord());
//        return "用户名：" + user.getUserName () + ", 密码: " + user.getPassWord();
//        QueryWrapper<userInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("userName", user.getUserName()); // 构建查询条件
//       List<userInfo> list= userMapper.selectList(queryWrapper);
        userInfo userInfo =userService.login(user);
        if(userInfo==null){
            throw new BizException(ResultCode.USER_LOGIN_ERROR);
        }else {
            HashMap<String, Object> data = new HashMap<>();
            data.put("info", userInfo);
            return data;
        }

    }



    //接收表单数据
    @PostMapping("/login")
    public String login(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord) {
        System.out.println("用户名：" + userName + ", 密码: " + passWord);
        return "用户名：" + userName + ", 密码: " + passWord;
    }

    @GetMapping("/fail")
    public Integer error() {
        int res = 0; // 查询结果数
        if( res == 0 ) {
            throw new BizException("没有数据");
        }
        return res;
    }

    //接收map数据
    @PostMapping("/loginMap")
    public String loginMap(@RequestParam Map<String, Object> map) {
        System.out.println("用户名：" + map.get("userName") + ", 密码: " + map.get("passWord"));
        return "mapping用户名：" + map.get("userName") + ", 密码: " + map.get("passWord");
    }

    //接收数组或List接收多个参数
    @PostMapping("/loginList")
    public String loginList(@RequestParam String[] n) {
        return Arrays.toString(n);
    }

    //接收数组或List接收多个参数
    @PostMapping("/loginListBody")
    public String loginListBody(@RequestBody String[] n) {
        return Arrays.toString(n);
    }

    //接收json数据参数
    @PostMapping("/loginJson")
    public String loginJson(@RequestBody Map<String, Object> map) {
        System.out.println("用户名：" + map.get("username") + ", 密码: " + map.get("passWord"));
        return "用户名：" + map.get("username") + ", 密码: " + map.get("passWord");
    }

    //接收GET请求参数：
    //参数可以为空 没有参数时为null
    @GetMapping("/getLogin")
    public String getLogin(@RequestParam(name = "name", required = false) String name) {
        return "获取到的name是：" + name;
    }

    //接收GET请求参数：
    //使用 map 来接收所有的请求参数：
    @GetMapping("/getLogin1")
    public String getLogin1(@RequestParam Map<String, Object> params) {
        return "name：" + params.get("name") + "<br>age：" + params.get("age");
    }

    //http://192.168.0.105:8081/api/page?pageNo=1&pageSize=10&sortBy&isAsc=false
    //分页查询
    @GetMapping("/page")
    @ResponseResult
    public  Map<String, Object>  queryUsersPage(UserQuery query){
        PageDTO<userInfo> info=userService.queryUsersPage(query);
        HashMap<String, Object> data = new HashMap<>();
        data.put("info", info);
        return data;
    }

}


